// packageB/pages/index/index.js
const app = getApp()
const INTERVAL = 10000;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    appurl: app.apiurl,
    apiimg: app.apiimg,
    ggid: '',
    gglist: '',
    jplist: '',
    name: '',
    navlist0: [
      '不限', '参赛人数多', '报名钻石少'
    ],
    type: 0,
    type1: 0,
    navlist1: [{
      "id": '', //ID
      "name": "不限" //奖品大类名称
    }],
    navlist2: [{
        id: "1",
        title: "无运费",
        xz_flag: false
      },
      {
        id: "2",
        title: "可兑换",
        xz_flag: false
      }
    ],
    saixuan: false,
    tanchuang12: false,
    tanchuang12_flag: false,
    tanchuang22: false,
    xinxi: '',
    page_no: 1,
    page_no1: 1
  },
  z_open() {
    this.setData({
      saixuan: true
    })
  },
  z_close() {
    let navlist2 = [{
        id: "1",
        title: "无运费",
        xz_flag: false
      },
      {
        id: "2",
        title: "可兑换",
        xz_flag: false
      }
    ]
    this.setData({
      type: 0,
      type1: 0,
      navlist2: navlist2
    })
  },
  z_wancheng() {
    this.setData({
      saixuan: false
    })
    this.z_jiangpin()
  },
  z_sea(e) {
    this.setData({
      name: e.detail.value
    })
  },
  z_jiangpin1() {
    this.z_jiangpin()
  },
  z_shaixuan1(e) {
    this.setData({
      type: e.currentTarget.dataset.index
    })
    this.z_jiangpin()
  },
  z_shaixuan2(e) {
    this.setData({
      type1: e.currentTarget.dataset.index
    })
  },
  z_shaixuan3: function (e) {
    let _this = this
    let index = e.currentTarget.dataset.index
    let xz_flag = e.currentTarget.dataset.xz_flag
    let id = e.currentTarget.dataset.id
    if (xz_flag) {
      _this.setData({
        [`navlist2[${index}].xz_flag`]: false,
      })
      let arr = _this.data.vip_xz.indexOf(id)
      let vip_xz = _this.data.vip_xz
      vip_xz.splice(arr, 1)
      _this.setData({
        vip_xz: vip_xz
      })
    } else {
      _this.setData({
        [`navlist2[${index}].xz_flag`]: true
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this
    this.timer = setInterval(function () {
      that.z_kaihei3()
      // 这里可以执行定时器需要执行的任务
    }, INTERVAL);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.guanggao1()
    this.z_jiangpin()
    // this.z_jilu2()
    this.z_yonghu()
    this.z_idxgg()
    this.z_jilu3()
    this.z_jilu4()
    this.z_kaihei3()
  },
  z_kaihei3() {
    let _this = this
    let params = {
      "token": wx.getStorageSync("user_token")
    }
    wx.request({
      url: app.apiurl + 'api/duobao/duobaoTanChuangInfo',
      method: 'GET',
      data: params,
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 修改请求
      },
      success(res) {
        if (res.data.data != '') {
          _this.setData({
            xinxi: res.data.data
          })
          // if (wx.getStorageSync("jiangpin") != res.data.data.duobao_id) {
            if(!_this.data.tanchuang12 && !_this.data.tanchuang12_flag){
              wx.setStorageSync("jiangpin", res.data.data.duobao_id)
              wx.setStorageSync("jiangpin_prize_id", res.data.data.prize_id)
              _this.setData({
                tanchuang12: true,
              })
            }
          // }
        }
      }
    })
  },
  z_kaihei4() {
    let _this = this
    _this.setData({
      tanchuang12: false,
      tanchuang12_flag: true,
      tanchuang22: true
    })
  },
  z_kaihei5() {
    let _this = this
    let params = {
      "token": wx.getStorageSync("user_token"),
      "duobao_id": wx.getStorageSync("jiangpin"),
      "prize_id": wx.getStorageSync("jiangpin_prize_id"),
      "lj_remark": "",
      "name": _this.data.yonghu.shr_name,
      "tel": _this.data.yonghu.shr_tel,
      "address": _this.data.yonghu.yj_addr
    }
    wx.request({
      url: app.apiurl + 'api/duobao/duobaoReceivePrize',
      method: 'POST',
      data: params,
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 修改请求
      },
      success(res) {
        _this.setData({
          tanchuang22: false,
          tanchuang12_flag:false
        })
      }
    })
  },
  z_jilu3() {
    let _this = this
    let params = {
      "token": wx.getStorageSync("user_token")
    }
    wx.request({
      url: app.apiurl + 'api/config/unprizeList?fbwz_type=5',
      method: 'GET',
      data: params,
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 修改请求
      },
      success(res) {
        _this.setData({
          mylist: res.data.data
        })
      }
    })
  },
  z_jilu4() {
    let _this = this
    let params = {
      "token": wx.getStorageSync("user_token")
    }
    wx.request({
      url: app.apiurl + 'api/config/dhjpdlList',
      method: 'GET',
      data: params,
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 修改请求
      },
      success(res) {
        let navlist1 = [{
          "id": '', //ID
          "name": "不限" //奖品大类名称
        }]
        for (let i = 0; i < res.data.data.length; i++) {
          navlist1.push(res.data.data[i])
        }
        _this.setData({
          navlist1: navlist1
        })
      }
    })
  },
  guanggao1() {
    let _this = this
    let params = {
      "token": wx.getStorageSync("user_token"),
    }
    wx.request({
      url: app.apiurl + 'api/config/advertPage',
      method: 'POST',
      data: params,
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 修改请求
      },
      success(res) {
        console.log(res.data.data)
        _this.setData({
          ggid: res.data.data[0].id
        })
        _this.guanggao2()
      }
    })
  },
  guanggao2() {
    let _this = this
    let params = {
      "token": wx.getStorageSync("user_token"),
    }
    wx.request({
      url: app.apiurl + 'api/config/advert?page_id=4',
      method: 'GET',
      data: params,
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 修改请求
      },
      success(res) {
        _this.setData({
          gglist: res.data.data[0]
        })
      }
    })
  },
  z_kvlianjia(e){
    let _this = this
    let link_url = e.currentTarget.dataset.url
    let id = e.currentTarget.dataset.url
    if(link_url != undefined){
      wx.navigateTo({
        url: link_url,
      })
    }
  },
  z_jiangpin() {
    let _this = this
    let con_type
    if (_this.data.type1 == 0) {
      con_type = '0'
    } else if (_this.data.type1 == 1) {
      con_type = '2'
    } else if (_this.data.type1 > 1) {
      con_type = '1'
    }
    let jpdl
    if (_this.data.type1 > 1) {
      jpdl = _this.data.navlist1[_this.data.type1].id
    } else {
      jpdl = '0'
    }
    let params = {
      "token": wx.getStorageSync("user_token"),
      "page_no": _this.data.page_no, //第几页
      "page_num": "500", //每页显示数量
      "jpdl": jpdl, //奖品大类id
      "name": _this.data.name, //奖品名称
      "order_type": _this.data.type, //排序  0 不限制   1-兑换数量降序   2-兑换钻石升序
    }
    wx.request({
      url: app.apiurl + 'api/duobao/duobaoPrizeList',
      method: 'POST',
      data: params,
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 修改请求
      },
      success(res) {
        _this.setData({
          jplist: res.data.data.list,
          saixuan: false
        })
      }
    })
  },
  z_jilu2() {
    let _this = this
    let params = {
      "token": wx.getStorageSync("user_token"),
    }
    wx.request({
      url: app.apiurl + 'api/member/lotteryRecord?page_no=' + _this.data.page_no1 + '&page_num=10&type=2',
      method: 'GET',
      data: params,
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 修改请求
      },
      success(res) {
        _this.setData({
          list2: res.data.data.list
        })
      }
    })
  },
  z_yonghu() {
    let _this = this
    let params = {
      "token": wx.getStorageSync("user_token"),
    }
    wx.request({
      url: app.apiurl + 'api/member/myInfo',
      method: 'POST',
      data: params,
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 修改请求
      },
      success(res) {
        if (res.data.code == '1001') {
          _this.setData({
            login: true,
            yonghu: res.data.data
          })
        }
        if (res.data.data.yd_status == 3) {
          _this.z_yindao()
        }
      }
    })
  },
  z_yindao() {
    let _this = this
    let params = {
      "token": wx.getStorageSync("user_token"),
      "yd_status": '4'
    }
    wx.request({
      url: app.apiurl + 'api/member/upMyYd',
      method: 'POST',
      data: params,
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 修改请求
      },
      success(res) {}
    })
  },
  z_idxgg() {
    let _this = this
    let params = {
      "token": wx.getStorageSync("user_token"),
    }
    wx.request({
      url: app.apiurl + 'api/home/index',
      method: 'POST',
      data: params,
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 修改请求
      },
      success(res) {
        _this.setData({
          login: true,
          idx_gg: res.data.data,
          // time1:res.data.data.activity_info.diff_time
        })
        // _this.z_time()
      }
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    if (this.timer) {
      clearInterval(this.timer);
    }
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    if (this.timer) {
      clearInterval(this.timer);
    }
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    //下滑刷新
    //  let _this = this
    //  let page_no  = _this.data.page_no
    //  page_no = page_no+1
    //  let page_no1  = _this.data.page_no1
    //  page_no1 = page_no1+1
    //  _this.setData({
    //    page_no:page_no,
    //    page_no1:page_no1
    //  })
    //  _this.z_jiangpin()
    //  _this.z_jilu1()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})